package com.notion.algorithms.datastructures;

public class MinHeapAdapter<T extends Comparable<T>>  {

	private MaxHeap<MinHeapElementAdapter> maxHeap;
	
	public MinHeapAdapter(int noOfElements){
		maxHeap = new MaxHeap<MinHeapElementAdapter>(noOfElements);		
	}
	public void insert(T element){
		maxHeap.insert(new MinHeapElementAdapter(element));
	}
	
	public T extractMin(){
		MinHeapElementAdapter element = maxHeap.extractMax();
		return element!=null?
				element.getElement():
					null;
	}
	
	private class MinHeapElementAdapter implements Comparable<MinHeapElementAdapter>{
		T element;
		public MinHeapElementAdapter(T element){
			this.element = element;
		}
		//Reverse comparisin to fool maxheap to return min heap results
		public int compareTo(MinHeapElementAdapter o) {
			return o.getElement().compareTo(this.getElement());
		}
		
		public T getElement(){
			return element;
		}
	}
}
